package entity

import (
	"fmt"
	"github.com/mangenotwork/common/log"
	"math/rand"
	"time"
)

// 扑克牌的定义
const (
	PokerA          = "A"
	Poker2          = "2"
	Poker3          = "3"
	Poker4          = "4"
	Poker5          = "5"
	Poker6          = "6"
	Poker7          = "7"
	Poker8          = "8"
	Poker9          = "9"
	Poker10         = "10"
	PokerJ          = "J"
	PokerQ          = "Q"
	PokerK          = "K"
	PokerJokerSmall = "JokerSmall"
	PokerJokerBig   = "JokerBig"
)

var PokerStrList = []string{PokerA, Poker2, Poker3, Poker4, Poker5, Poker6, Poker7, Poker8, Poker9, Poker10, PokerJ, PokerQ, PokerK}

var PokerStrNumber = map[string]int{
	PokerA: 1, Poker2: 2, Poker3: 3, Poker4: 4, Poker5: 5, Poker6: 6, Poker7: 7, Poker8: 8, Poker9: 9, Poker10: 10, PokerJ: 11, PokerQ: 12, PokerK: 13, PokerJokerSmall: 14, PokerJokerBig: 15,
}

// 牌的点数
// 纸牌斗牛规则里：大小王除外，52张牌，K最大，A最小，J，Q，K都是10点，然后点数依次排列最后A是1点
var PokerValue = map[string]int{
	PokerA: 1, Poker2: 2, Poker3: 3, Poker4: 4, Poker5: 5, Poker6: 6, Poker7: 7, Poker8: 8, Poker9: 9, Poker10: 10, PokerJ: 10, PokerQ: 10, PokerK: 10,
}

// 花色 黑桃>红桃>梅花>方块
const (
	HeiTao    int = 1
	HongTao   int = 2
	MeiHua    int = 3
	FangKuang int = 4
)

var HuaSeList = []int{HeiTao, HongTao, MeiHua, FangKuang}

var HuaSeStr = map[int]string{
	HeiTao:    "♠",
	HongTao:   "♥",
	MeiHua:    "♣",
	FangKuang: "♦",
}

type PokerData struct {
	Poker       string `json:"poker"`        // 牌
	PokerNumber int    `json:"poker_number"` // 牌面号
	HuaSe       int    `json:"hua_se"`       // 花色   红桃:1  梅花:2  方框:3  黑桃:4
	PokerStr    string `json:"poker_str"`    // 牌字符串
	Points      int    `json:"points"`       // 点数
}

func (p *PokerData) Show() {
	log.Info(p.PokerStr)
}

// 新建立一幅牌
func NewPoker() []*PokerData {
	pokerList := make([]*PokerData, 0)
	for _, i := range PokerStrList {
		for _, j := range HuaSeList {
			pokerList = append(pokerList, &PokerData{
				Poker:       i,
				PokerNumber: PokerStrNumber[i],
				HuaSe:       j,
				PokerStr:    fmt.Sprintf("%s%s", HuaSeStr[j], i),
				Points:      PokerValue[i],
			})
		}
	}
	//pokerList = append(pokerList, &PokerData{
	//	Poker:    PokerJokerBig,
	//	HuaSe:    0,
	//	PokerStr: "JokerBig",
	//})
	//pokerList = append(pokerList, &PokerData{
	//	Poker:    PokerJokerSmall,
	//	HuaSe:    0,
	//	PokerStr: "JokerSmall",
	//})
	return ShuffleSlice(pokerList)
}

// 洗牌算法
func ShuffleSlice(slice []*PokerData) []*PokerData {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := range slice {
		j := r.Intn(i + 1)                      // 生成0到当前索引之间的随机数
		slice[i], slice[j] = slice[j], slice[i] // 交换位置
	}
	return slice
}

// 扑克对象转字符串
func PokerToStr(poker []*PokerData) string {
	str := ""
	for _, v := range poker {
		str += v.Poker
	}
	return str
}

// 扑克牌字符串转点数
func PokerStrToPoints(str string) int {
	return PokerValue[str]
}

// 扑克牌字符的牌面号
func GetPokerStrNumber(str string) int {
	return PokerStrNumber[str]
}

// 在一副牌中拿走一张牌返回原牌
func TakeOnePoker(poker []*PokerData, p *PokerData) []*PokerData {
	for i := 0; i < len(poker); i++ {
		if poker[i].Poker == p.Poker && poker[i].HuaSe == p.HuaSe {
			poker = append(poker[0:i], poker[i+1:len(poker)]...)
		}
	}
	return poker
}

func PrintPoker(poker []*PokerData) {
	str := ""
	for _, p := range poker {
		str += p.PokerStr
	}
	log.Info("PrintPoker : ", str)
}

// 扑克对象转字符串
func PokerToShow(poker []*PokerData) string {
	str := ""
	for _, v := range poker {
		str += v.PokerStr
	}
	return str
}
